package jogs.client.games;

import java.util.ArrayList;
import java.util.Collections;
import java.lang.Integer;
import java.io.Serializable;

public class Card implements Serializable
{
	private static final long serialVersionUID = 5L;
	private final int rank;
	private final char suit;
	private static final String SUITS = "dhsc";
	
	public Card(char suit, int rank)
	{
		this.rank = rank;
		this.suit = suit;
	}

  public Card(String s)
  {
    this.suit = s.charAt(s.length() - 1); 
    this.rank = Integer.parseInt(s.substring(0, s.length()-1)); 
  }
	
	public char getSuit()
	{
		return suit;	
	}
	
	public int getRank()
	{
		return rank;	
	}
	
	public String getSuitString()
	{
		String s = "";
		switch( suit)
		{
		case 'd':
			s = "dimonds";
			break;
		case 'h':
			s = "harts";
			break;
		case 'c':
			s = "clubs";
			break;
		case 's':
			s = "spades";
		}	
		return s;
	}
	
	@Override
	public String toString()
	{
		String r = Integer.toString(rank) + String.valueOf(suit);	
		return r;
	}
	
	public boolean equals(Card c)
	{
		if(c.getSuit() == this.getSuit() && c.getRank() == this.getRank())
			return true;
		return false;
	}
	
	@Override
	public boolean equals(Object o)
	{
		Card c = (Card)o;
		return this.equals(c);
	}
	
	
	public static ArrayList<Card> newDeck()
	{
		ArrayList<Card> deck = new ArrayList<Card>();
		for(int i=1; i < 14; i++)
		{
			deck.add(new Card('s', i));
			deck.add(new Card('c', i));
			deck.add(new Card('h', i));
			deck.add(new Card('d', i));
		}
		Collections.shuffle(deck);
		return deck;
	}
	
	public static Card createCard(String card)
	{
		Card r = null;
		int length = card.length();
		char suit = card.charAt(length - 1);
		int rank = -1;
		try
		{
			rank = Integer.parseInt(card.substring(0, length - 1));
		}
		catch(Exception e)
		{
			return null;
		}
		if((rank > 0 && rank < 14) && (SUITS.indexOf(suit) != -1))
			r = new Card(suit, rank);
		return r;
	}
	
}
